package com.phpxiu.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.RelativeLayout;

import com.phpxiu.app.adapter.holder.RoomFansRankViewHolder;
import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.Fans;
import com.phpxiu.app.model.list.HomeHot;
import com.phpxiu.app.model.list.RoomRankFansInfo;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.ListEmptyView;

import java.util.List;

/**
 * Created by WuJinZhou on 2016/5/30.
 */
public class RoomFansRankListAdapter extends BaseAdapter<RoomRankFansInfo> {
    private RoomRankFansInfo empty=new RoomRankFansInfo();
    private ListEmptyView emptyView;
    private String prefix,unit;
    private RelativeLayout.LayoutParams avatarViewParam;
    private int bigSize,smallSize;
    public RoomFansRankListAdapter(Context context, List<RoomRankFansInfo> data) {
        super(context, data);
        prefix=context.getString(R.string.ui_rank_contribute);
        unit=context.getString(R.string.ticket_unit);
        empty.setListViewType(RoomRankFansInfo.EMPTY_VIEW);
        data.add(0,empty);
        emptyView=new ListEmptyView(context);
        emptyView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setMsg(context.getString(R.string.list_empty_title));
        emptyView.setPadding(0,KKYUtil.dip2px(context,100),0,0);
        bigSize= KKYUtil.dip2px(context,55);
        smallSize=KKYUtil.dip2px(context,36);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RoomRankFansInfo item=data.get(position);
        if(item.getListViewType()==RoomRankFansInfo.EMPTY_VIEW){
            return emptyView;
        }
        Fans user=item.getUser();
        RoomFansRankViewHolder holder=null;
        if(convertView==null||convertView==emptyView){
            convertView=mInflater.inflate(R.layout.room_fans_rank_list_item_view,null);
            holder=new RoomFansRankViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder=(RoomFansRankViewHolder)convertView.getTag();
        holder.setRank(position);
        avatarViewParam=(RelativeLayout.LayoutParams)holder.avatarView.getLayoutParams();
        if(position<3){
            avatarViewParam.height=bigSize;
            avatarViewParam.width=bigSize;
        }else{
            avatarViewParam.height=smallSize;
            avatarViewParam.width=smallSize;
        }
        holder.avatarView.setLayoutParams(avatarViewParam);
        holder.avatarView.setImageURI(Uri.parse(user.getAvatar()));
        holder.nickView.setText(user.getNickname());
        if("1".equals(user.getSex())){
            holder.sexIconView.setImageResource(R.mipmap.user_sex_icon_male);
        }else{
            holder.sexIconView.setImageResource(R.mipmap.user_sex_icon_female);
        }
        holder.lvIconView.setImageURI(Uri.parse("asset:///lvIcons/" +user.getLvl()+ ".png"));
        holder.setTicketsCountView(prefix, item.getTotal(), unit);
        holder.showDivider();
        if(data.size()>0&&position==data.size()-1){
            holder.hideDivider();
        }
        return convertView;
    }

    /**
     * 刷新列表
     */
    public void refresh() {
        if(emptyView!=null){
            emptyView.loaded();
        }
        if(data.contains(empty)){
            data.remove(empty);
        }
        if(data.size()==0){
            data.add(empty);
        }
        super.notifyDataSetChanged();
    }
}
